import { ref } from 'vue'
import { defineStore } from 'pinia'
import storage from '@/utils/storage'

export const useUserStore = defineStore(
  'manager-user',
  () => {
    const state = ref({
      //获取用户信息
      userInfo: storage.getItem('userInfo') || {},
      // 获取菜单列表
      menuList: storage.getItem('menuList') || [],
      actionList: storage.getItem('actionList'),
      noticeCount: 0
    })
    //保存用户信息
    const saveUserInfo = (userInfo) => {
      state.value.userInfo = userInfo
      storage.setItem('userInfo', userInfo)
    }

    //保存菜单信息
    const saveMenuList = (menuList) => {
      state.value.menuList = menuList
      storage.setItem('menuList', menuList)
    }

    // 保存action信息
    const saveActionList = (actionList) => {
      state.value.actionList = actionList
      storage.setItem('actionList', actionList)
    }

    // 保存信息通知总数
    const saveNoticeCount = (noticeCount) => {
      state.value.noticeCount = noticeCount
      storage.setItem('noticeCount', noticeCount)
    }

    // 通知总数-1
    const decreNoticeCount = (noticeCount) => {
      state.value.noticeCount = noticeCount - 1
      storage.setItem('noticeCount', noticeCount)
    }

    return {
      state,
      saveUserInfo,
      saveMenuList,
      saveActionList,
      saveNoticeCount,
      decreNoticeCount
    }
  },
  {
    //持久化存储
    persist: true
  }
)
